package com.qf.business.system.controller;

import com.qf.business.system.power.Auth;
import com.qf.business.system.service.PropagandaService;
import com.qf.commons.web.utils.UploadUtils;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.system.entity.Employee;
import com.qf.data.system.entity.Propaganda;
import com.qf.data.system.vo.input.PropagandaInput;
import com.qf.data.system.vo.output.ProLoadOutput;
import com.qf.data.system.vo.output.PropagandaOutput;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * (Propaganda)表控制层
 *
 * @author makejava
 * @since 2022-07-01 10:00:55
 */
@RestController
@RequestMapping("/propaganda")
@Slf4j
public class PropagandaController {

    @Autowired
    private PropagandaService propagandaService;

    //图片路径
    @Value("${filename.path}")
    private String filepath;

    @RequestMapping("/list")
    public R proList(){
        List<Propaganda> propagandas = propagandaService.list();

        List<PropagandaOutput> outputs = propagandas.stream()
                .map(p ->{
                    PropagandaOutput propagandaOutput = new PropagandaOutput();
                    BeanUtils.copyProperties(p,propagandaOutput);
                    return  propagandaOutput;
                }).collect(Collectors.toList());
        return Rs.create(outputs);
    }

    @RequestMapping("/insert")
    public R insert( PropagandaInput propagandaInput){
        Propaganda propaganda = QfBeanUtils.beanCopy(propagandaInput, Propaganda.class);
        propagandaService.save(propaganda);
        return Rs.create("succ");
    }

    @RequestMapping("/upload")
    @Auth(mustLogin = false)
    public R upLoadFile(MultipartFile file,HttpServletRequest request,ProLoadOutput proLoadOutput){

        //file是一个临时文件，需要转存到指定位置，否则本次请求完成后临时文件会删除
        log.info(file.toString());
        //原始文件
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileName = UUID.randomUUID().toString() + suffix;
        log.info("[fileName] - {}",fileName);
        //获取存储地址
        File uploadFlie = new File(filepath);
        log.info("[uploadFile] - {}",uploadFlie);
        if(!uploadFlie.exists()){
            uploadFlie.mkdirs();
        }

        try {
            file.transferTo(new File(filepath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Rs.create(fileName);
    }

    @RequestMapping("/getloadfile")
    public void getLoadFile(String name, HttpServletResponse response){
        try(
                InputStream in = new FileInputStream(new File(filepath,name));
                OutputStream out = response.getOutputStream();
         ){
             IOUtils.copy(in,out);
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}

